package com.sky.utils;
import org.mindrot.jbcrypt.BCrypt;
public class BCryptUtil {
        // 生成密码哈希
        public static String hashPassword(String plainPassword) {
            // 生成盐值
            String salt = BCrypt.gensalt();

            // 使用盐值和密码生成哈希值
            return BCrypt.hashpw(plainPassword, salt);
        }

        // 验证密码
        public static boolean checkPassword(String plainPassword, String hashedPassword) {
            // 使用哈希值进行密码验证
            return BCrypt.checkpw(plainPassword, hashedPassword);
        }

        public static void main(String[] args) {
            // 示例
            String passwordToHash = "123456";

            // 生成哈希值
            String hashedPassword = hashPassword(passwordToHash);
            System.out.println("Hashed Password: " + hashedPassword);

            // 验证密码
            String inputPassword = "123456";
            if (checkPassword(inputPassword, hashedPassword)) {
                System.out.println("Password is valid");
            } else {
                System.out.println("Password is invalid");
            }
        }
    }

